
def options(opt):
    libraries = ["openmpi", "intelmpi", "mpich"]
    opt.compiler.add_option("--mpi-lib",
        action="store",
        metavar=libraries,
        choices=libraries,
        help="Set the MPI library version. Use this option when the auto-detection mechanism fails.")

def configure(ctx):
    # Open MPI and Intel-MPI are based on MPICH, we need to check MPICH_VERSION at last
    code = '''
        #include "mpi.h"
        #include <cstdio>
        int main() {
        #ifdef OMPI_MAJOR_VERSION
            printf("Open MPI");
            return 0;
        #endif
        #ifdef I_MPI_VERSION
            printf("Intel MPI");
            return 0;
        #endif
        #ifdef MPICH_VERSION
            printf("mpich");
            return 0;
        #endif
            printf("unknown");
            return 0;
        }
    '''

    ctx.env.MPI = ctx.check_cxx(
        mandatory   = False,
        msg         = 'Checking for MPI library',
        fragment    = code,
        define_ret  = True,
        execute     = True,
        okmsg       = lambda ret: str(ret["success"]))
